import '../public/index'
import url from '../data/url'
import request from '../data/request'
import { tableConfig,header } from '../data/config'
import { getUrlParam } from '../data/util'
import store from 'store'

const base          = url.base,
      getUserPage  = request.getUserPage,
      resetUserPwd = request.resetUserPwd

let table,
    storeData

// 初始化表格
table = $('#table').bootstrapTable($.extend({}, tableConfig, {
  url: base + getUserPage,
  queryParams: function (params) {
    return $.extend({
      currentPage : params.pageNumber,
      pageSize    : params.pageSize
  },$('#searchForm').serializeObject())
  },
  columns: [
    {
      field: 'loginName',
      title: '用户名',
      valign: 'middle'
    }, {
      field: 'realName',
      title: '真实姓名',
      valign: 'middle'
    }, {
      field: 'email',
      title: '电子邮箱',
      valign: 'middle'
    }, {
      field: 'phone',
      title: '手机号码',
      valign: 'middle'
    }, {
      field: 'type',
      title: '用户类型',
      valign: 'middle',
      formatter: (index, row) => {
        switch (index){
          case 0:
            return '个人用户'
          break;
          case 1:
            return '企业管理员'
            break;
          case 2:
            return '企业操作员'
            break;
          case 3:
            return '平台管理员'
            break;
          case 4:
            return '平台运营人员'
            break;
          case 5:
            return '代理商'
            break;
          case 6:
            return '合作商铺'
            break;
          case 7:
            return '经济人'
            break;
          case 8:
            return '银行运营员'
            break;
          case 9:
            return '职业顾问'
            break;
          case 10:
            return '案场经理'
            break;
          default:
            return '未知'
        }
      }
    }, {
      field: 'enterpriseName',
      title: '所属企业',
      valign: 'middle'
    }, {
      field: 'action',
      valign: 'middle',
      align: 'center',
      width: '260px',
      title: '操作',
      formatter: function(index, row) {
        return `<div class="btn-group">
                      <button type="button" data-id="${ row.idStr }" class="btn btn-default btn-modify">修改</button>                  
                      <button type="button" data-id="${ row.idStr }" class="btn btn-default btn-detail">详情</button>                  
                      <button type="button" data-id="${ row.idStr }" class="btn btn-default btn-resetPwd">重置密码</button>                  
                </div>`
      }
    }]
}))

// 点击列表的时候传递数据到全局变量
table.on('click-row.bs.table', (e, data, el) => {
  storeData = data
})

// 搜索
$('#search-btn').on('click', (e) => {
  e.preventDefault();

  table.bootstrapTable('refresh', {
    query: $.extend({
      currentPage : 1,
    }, $('#searchForm').serializeObject())
  })
})

// 重置密码
$(document).on('click', '#table .btn-resetPwd', (e) => {
  e.preventDefault()

  console.log('重置密码')

  let id     = $(e.target).attr('data-id'),
      data   = {userId: id};

  bootbox.confirm({
    title: '提示',
    message: `<div>
                <h3 style="margin:.75em 0">是否重置该用户密码？</h3>
              </div>`,
    buttons: {
      cancel: {
        label: '取消'
      },
      confirm: {
        label: '重置'
      }
    },
    callback: (result) => {

      if(!result){
        return
      }

      let loading_modal = layer.msg('提交中...', {
        icon: 16,
        time: 0
      })

      $.ajax({
        headers: header,
        url : base + resetUserPwd,
        type: 'POST',
        data: data
      })
        .done(function (res) {
          console.log(res)

          if( res.code == 0 ){

            layer.msg('密码已重置为123456，用户首次登录将提示修改!', {
              icon: 6
            })

            table.bootstrapTable('refresh')

          }else {
            layer.msg('重置密码失败', {
              icon: 5
            })
          }

        })
        .fail(function (res) {
          console.log(res)

          layer.msg('重置密码失败', {
            icon: 5
          })

        })
        .always(function(res) {

          console.log("complete")

          layer.close(loading_modal)
        })
    }
  })
})

//跳转至修改页
$(document).on('click', '#table .btn-modify', (e) => {
  e.preventDefault()

  console.log('修改')
  store.set('storeData', storeData)
  let id = $(e.target).attr('data-id');
  window.location.href = './modify.html?id=' + id;
})

//跳转至详情页
$(document).on('click', '#table .btn-detail', (e) => {
  e.preventDefault()

  console.log('详情')
  store.set('storeData', storeData)

  let id = $(e.target).attr('data-id')
  window.location.href = `./detail.html?id=${id}`
})